package com.gdl.yygh.cmn.controller;

import com.gdl.yygh.cmn.service.DictService;
import com.gdl.yygh.common.exception.HospitalException;
import com.gdl.yygh.common.result.Result;
import com.gdl.yygh.common.result.ResultCodeEnum;
import com.gdl.yygh.hospital.model.cmn.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;


    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
          dictService.exportData(response);
    }
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    //        return result(flag);
    private Result result(boolean flag) {
        if (flag)
            return Result.ok();
        else
            return Result.fail();
    }


    @GetMapping("test")
    public Result test() {
        try {
            int a = 1 / 0;
        } catch (Exception exception) {
            throw new HospitalException(ResultCodeEnum.URL_ENCODE_ERROR);
        }
        return Result.ok();
    }

    @PostMapping("login")
    public Result login( ) {

        return Result.ok("admin-token");
    }
    @GetMapping("info")
    public Result info( ) {

        Map<String,Object> map=new HashMap<>();

        List<String> list=new ArrayList<>();
        list.add("admin");
        map.put("roles",list);

        map.put("introduction","I am a super administrator");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name","Super Admin");

        return Result.ok(map);
    }
}
